<?php


namespace app\model;


use think\facade\Db;
use think\Model;
use \app\model\DeliveryModel;
use app\model\QuotationModel;
use app\model\QuotationTransportModel;
class DeliveryAttachModel extends Model
{
    protected $table = "store_delivery_attach";

    /**
     * @param $insert_id
     * @return string
     * @notes 附属表插值
     * @author: dongdong
     * @date: 2025/1/8 16:16
     */
    public function add($insert_id){
        Db::startTrans();
        try {
            $find = self::where(['delivery_id'=>$insert_id])->field("id")->find();
            $DeliveryModel = new DeliveryModel();
            $demand = $DeliveryModel->where(['id'=>$insert_id])->value('demand');
            $is_transport = 0;
            if($demand=='1') {
                $is_transport = 1;
            }
            if(empty($find)){
                self::insert(['delivery_id'=>$insert_id,'is_transport'=>$is_transport]);
            }
            Db::commit();
            return $insert_id;
        }catch (\Exception $e){
            Db::rollback();
            return '';
        }
    }


    /**
     * @param $param
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @notes 承接用户列表选择
     * @author: dongdong
     * @date: 2025/1/13 11:19
     */
    public function undertake_user($param){
        $find = self::where(['delivery_id'=>$param['delivery_id']])->find();
        $cooperate_id = $find['cooperate_id'];
        $arr = [];
        if($find['is_transport']==1){
            $QuotationTransportModel= new QuotationTransportModel();
            if($cooperate_id){
                $group = $QuotationTransportModel->field("user_id")->where("id","in",$cooperate_id)->where(["delivery_id"=>$param['delivery_id']])->select();
                foreach ($group as $k =>&$v){
                    if(!in_array($v['user_id'],$arr)){
                        array_push($arr,$v['user_id']);
                    }
                }
            }
        }else{
            $QuotationModel = new QuotationModel();
            if($cooperate_id){
                $group = $QuotationModel->field("user_id")->where("id","in",$cooperate_id)->where(["delivery_id"=>$param['delivery_id']])->select();
                foreach ($group as $k =>&$v){
                    if(!in_array($v['user_id'],$arr)){
                        array_push($arr,$v['user_id']);
                    }
                }
            }
        }

        $list = [];
        if($arr){
            foreach ($arr as $k =>&$v){
                array_push($list,Db::connect("fast_mysql")->table("fa_user")->field("id,username,username_ru,mobile,level,avatar")->where(['id'=>$v])->find());
            }
        }

        return $list;
    }


    /**
     * @param $delivery_id
     * @notes 确定评价
     * @author: dongdong
     * @date: 2025/1/13 17:22
     */
    public function evaluate($delivery_id){
        self::where(['delivery_id'=>$delivery_id])->update(['is_evaluate'=>1]);
    }

}